<?php

namespace App\Http\Controllers\Api\Web;

use App\Http\Controllers\Api\Controller;
use App\Models\Cate;
use App\Models\Config;
use App\Models\Picture;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Ixudra\Curl\Facades\Curl;
use zgldh\QiniuStorage\QiniuStorage;
use Mews\Captcha\Facades\Captcha;

class CommonController extends Controller
{

    // 获取首页banner推荐位
    public function pictureList(Request $request)
    {
        $recommendInfo = Picture::getList($request);
        return $this->success($recommendInfo);
    }

    // 获取分类列表
    public function cateList(Request $request)
    {
        $cateList = Cate::getList($request);
        return $this->success($cateList);
    }

    // 树形结构分类
    public function treeList(Request $request)
    {
        $cates = Cate::getAllAsTree($request);
        return $this->success($cates);
    }

    // 上传文件-七牛
    public function uploadFile(Request $request)
    {
        // 判断是否有文件上传
        if ($request->hasFile('file')) {
            // 获取文件,file对应的是前端表单上传input的name
            $file = $request->file('file');
            // 初始化
            $disk = QiniuStorage::disk('qiniu');
            // 重命名文件
            $fileName = date('Ym') . '_' . md5(microtime(true) . rand()) . '.' . $file->getClientOriginalExtension();
            // 上传到七牛
            $bool = $disk->put('ylly/' . $fileName, file_get_contents($file->getRealPath()));
            // 判断是否上传成功
            if ($bool) {
                $path = $disk->downloadUrl('ylly/' . $fileName, 'https');
                return $this->success(['url' => $path]);
            }
            return $this->failed('上传失败');
        }
        return $this->failed('请选择文件');
    }

    // 上传文件-本地
    public function uploadFileV1(Request $request)
    {
        if ($request->hasFile('file')) {
            $file = $request->file('file');
            $fileName = time() . '_' . $file->getClientOriginalName();
            $path = $file->storeAs('uploads', $fileName, 'public');
            // 获取文件的完整URL
            $url = $request->getSchemeAndHttpHost() . Storage::url($path);
            return $this->success(['url' => $url, 'path' => Storage::url($path), 'name' => $file->getClientOriginalName()]);
        }
        return $this->failed('上传失败');
    }

    // 生成验证码
    public function createCaptha() {
        $captcha = Captcha::create('flat', true); 
        return $this->success($captcha);
    }

    // 获取接口调用凭证
    public function getToken()
    {
        if (Cache::has('ylly_xcx_token') && Cache::get('ylly_xcx_token')) {
            $token = Cache::get('ylly_xcx_token');
        } else {
            // 获取token
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
            $result = Curl::to(sprintf($url, env('WECHAT_MINIPROGRAM_APP_ID'), env('WECHAT_MINIPROGRAM_APP_SECRET')))->get();
            $result = json_decode($result, true);
            // {"access_token":"70_dB4BMLcBmgJCKWBmEUuQmmf8OIP1eRuUIdgnRWnaotAYp4sRBJTPW6h8WooK8fSd9Fp2kUBN0r_OUW0U0jBtS2ISGRN1SMSL2r4u2PqXLgOtpA4fY9os3Y_vHvkCYZiAIAJFG","expires_in":7200}
            if (isset($result['access_token']) && $result['access_token']) {
                $token = $result['access_token'];
                Cache::put('wallpaler_xcx_token', $token, 100);
            } else {
                return $this->failed($result['errmsg']);
            }
        }
        return $this->success($token);
    }

    // 获取配置信息
    public function configInfoByCode($code)
    {
        $cateInfo = Config::getInfoByCode($code);
        return $this->success($cateInfo);
    }

}
